package com.bdqn.security;

import com.bdqn.pojo.SysUser;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.Set;

/**
 * SecurityUser Spring Security安全用户
 *
 * @author LILIBO
 * @since 2025/4/7
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class SecurityUser implements UserDetails {

    /**
     * 真实的用户对象
     */
    private SysUser sysUser;
    /**
     * 用户拥有的权限集合
     */
    private Set<GrantedAuthority> authorities;

    /**
     * 构造方法
     *
     * @param username 用户名
     * @param password 密码
     * @param authorities 权限集合
     */
    public SecurityUser(String username, String password, Set<GrantedAuthority> authorities) {
        this.sysUser = new SysUser(username, password);
        this.authorities = authorities;
    }

    /**
     * 获取权限集合
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    /**
     * 获得用户密码
     */
    @Override
    public String getPassword() {
        return sysUser.getUsrPassword();
    }

    /**
     * 获得用户名
     */
    @Override
    public String getUsername() {
        return sysUser.getUsrName();
    }

    /**
     * 账户是否启用
     */
    @Override
    public boolean isEnabled() {
        return sysUser.getUsrFlag() == 1;
    }

}
